package com.crdeng.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crdeng.business.converter.ProductConverter;
import com.crdeng.business.entity.Product;
import com.crdeng.business.service.ProductService;
import com.crdeng.business.vo.PageVO;
import com.crdeng.business.vo.ProductStockVO;
import com.crdeng.business.vo.ProductVO;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.Result;
import com.crdeng.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.List;

/**
 * <p>
 * 产品
 * </p>
 *
 * @author crdeng
 * @since 2020-12-19
 */
@Api(value = "物资资料接口", tags = "物资资料接口")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "获取物资列表")
    @PostMapping("/getList")
    public Result getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                          @RequestParam(value = "categorys", required = false) String categorys,
                          @RequestBody ProductVO productVO) {
        isExistCategorys(categorys, productVO);
        IPage<Product> page = productService.getList(pageNum, pageSize, productVO);
        List<ProductVO> productVOList = ProductConverter.Converter2VOList(page.getRecords());
        List<ProductVO> list = ProductConverter.converter2CategoryKeys(productVOList);
        long total = page.getTotal();
        return Result.ok().data("list", list).data("total", total);
    }

    /**
     * 存在分类则构建搜索条件
     *
     * @param categorys
     * @param productVO
     */
    private void isExistCategorys(String categorys, ProductVO productVO) {
        if (categorys != null && !categorys.equals("")) {
            /*获取分类*/
            String[] category = categorys.split(",");
            switch (category.length) {
                case 1:
                    productVO.setOneCategoryId(Long.parseLong(category[0]));
                    break;
                case 2:
                    productVO.setOneCategoryId(Long.parseLong(category[0]));
                    productVO.setTwoCategoryId(Long.parseLong(category[1]));
                    break;
                case 3:
                    productVO.setOneCategoryId(Long.parseLong(category[0]));
                    productVO.setTwoCategoryId(Long.parseLong(category[1]));
                    productVO.setThreeCategoryId(Long.parseLong(category[2]));
                    break;
            }
        }
    }

    @ApiOperation(value = "添加物资资料")
    @PostMapping("/add")
    public Result add(@Validated @RequestBody ProductVO productVO) {
        productService.add(productVO);

        return Result.ok();
    }

    @ApiOperation(value = "编辑物资资料信息")
    @PutMapping("/edit")
    public Result edit(@Validated @RequestBody ProductVO productVO) {
        if (productVO.getId() == null) {
            throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(), ResultCode.PARAM_IS_BLANK.getMessage());
        }

        productService.edit(productVO);

        return Result.ok();
    }


    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam Long id) {
        productService.alterStatus(id, 1);
        return Result.ok();
    }

    @ApiOperation(value = "恢复和通过")
    @GetMapping("/recover")
    public Result recover(@RequestParam Long id) {
        productService.alterStatus(id, 0);
        return Result.ok();
    }

    @ApiOperation(value = "回收站")
    @GetMapping("/recycle")
    public Result recycle(@RequestParam Long id) {
        productService.alterStatus(id, 2);
        return Result.ok();
    }


    /**
     * 可入库物资(入库页面使用)
     * @return
     */
    @ApiOperation(value = "可出库物资列表", notes = "物资列表,根据物资名模糊查询")
    @PostMapping("/findProducts")
    public Result findProducts(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize" ,defaultValue = "6") Integer pageSize,
                                     @RequestParam(value = "categorys", required = false) String categorys,
                                     @RequestBody ProductVO productVO){
        productVO.setStatus(0);
        isExistCategorys(categorys, productVO);
        IPage<ProductStockVO> page = productService.findProductStocks(pageNum, pageSize, productVO);
        return Result.ok().data("list",page.getRecords()).data("total",page.getTotal());
    }


}

